﻿@* Generator: MvcHelper *@
@using System.Web.Mvc

@helper CombineClasses(ViewDataDictionary viewData, string classes)
{
    if (viewData.Keys.Any(k => k.ToLowerInvariant() == "class"))
    {
        viewData["class"] += " " + classes;
    }
    else
    {
        viewData.Add("class", classes);
    }
}

@helper AddTo(ViewDataDictionary viewData, string tagName, object tagValue)
{
    if (viewData.Keys.Any(k => k.ToLowerInvariant() == tagName))
    {
        viewData[tagName] += " " + tagValue;
    }
    else
    {
        viewData.Add(tagName, tagValue);
    }
}

@helper CheckForModelMetadata(ViewDataDictionary viewData)
{
    var metadata = viewData.ModelMetadata;
    var prop = metadata.ContainerType.GetProperty(metadata.PropertyName);
    var attrs = prop.GetCustomAttributes(false);

    var maxLength = attrs.OfType<System.ComponentModel.DataAnnotations.MaxLengthAttribute>().FirstOrDefault();
    if (maxLength != null)
    {
        @AddTo(viewData, "maxLength", maxLength.Length);
    }

    var trimLeadingTrailingSpaces = attrs.OfType<AdditionalMetadataAttribute>().FirstOrDefault(a => a.Name == "TrimLeadingTrailingSpaces");
    if (trimLeadingTrailingSpaces != null && (bool)trimLeadingTrailingSpaces.Value)
    {
        @CombineClasses(viewData, "trim-leading-trailing-spaces");
    }
}